package com.ht.api.call.func.impl.transfer;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.ht.api.bean.AjaxResult;
import com.ht.api.bean.LoginBean;
import com.ht.api.call.func.IFunctionCall;
import com.ht.api.db.Db;
import com.ht.api.db.DbList;
import com.ht.api.db.DbPage;
import com.ht.api.db.Entity;
import com.ht.api.db.exception.HtException;
import com.ht.api.service.TransferService;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 随机分配接替人员的需要继承的用户
 */
public class RandomCustomerWithNormalUserFunctionCall implements IFunctionCall {

	@Override
	public String getName() {
		return "随机分配接替人员的需要继承的用户";
	}

	@Override
	public JSONObject execute(LoginBean me, JSONObject param, AjaxResult result) {
		String transferId = param.getStr("transfer_id");

		Entity wxTransferUser = Db.createSqlParser("wx_transfer_user")
				.select("main_id,appid,type,handover_userid,takeover_userid,transfer_time,status").where("main_id", transferId).findOne(me.getDataSource());

		String appid = wxTransferUser.getStr("appid");
		String takeoverUserid = wxTransferUser.getStr("takeover_userid");
		String handoverUserid = wxTransferUser.getStr("handover_userid");
		String transferTime = wxTransferUser.getStr("transfer_time");
		int type = wxTransferUser.getInt("type");

		DbPage dbPage = TransferService.getHandoverUserInheritCustomers(me.getDataSource(), param);

		DbList list = dbPage.getRecord();

		if (list.size() == 0) {
			throw new HtException("添加失败，没有匹配到用户！");
		}

		List<Entity> dataList = new ArrayList<>();
		for (Entity entity : list) {
			Entity wxTransferUserItem = Db.createEntity("wx_transfer_user_item");
			wxTransferUserItem.set("appid", appid);
			wxTransferUserItem.set("transfer_id", transferId);
			wxTransferUserItem.set("handover_userid", handoverUserid);
			wxTransferUserItem.set("takeover_userid", takeoverUserid);
			wxTransferUserItem.set("external_userid", entity.getStr("external_userid"));
			wxTransferUserItem.set("status", 0);

			dataList.add(wxTransferUserItem);

			if (type == 1) {
				Entity workuserDimissionExternal = Entity.create("wx_workuser_dimission_external");
				workuserDimissionExternal.set("appid", appid);
				workuserDimissionExternal.set("handover_userid", handoverUserid);
				workuserDimissionExternal.set("external_userid", entity.getStr("external_userid"));
				workuserDimissionExternal.set("takeover_userid", takeoverUserid);
				workuserDimissionExternal.set("transfer_time", transferTime);
				workuserDimissionExternal.set("statue", 6);
				dataList.add(workuserDimissionExternal);
			}
		}

		if (type == 1) {
			Entity wxWorkuserDimission = Entity.create("wx_workuser_dimission");
			wxWorkuserDimission.set("appid", appid);
			wxWorkuserDimission.set("handover_userid", handoverUserid);
			wxWorkuserDimission.set("wait_cnt", -list.size());
			dataList.add(wxWorkuserDimission);
		}

		wxTransferUser.set("all_cnt", list.size());
		dataList.add(wxTransferUser);

		Db.insertBatchWithStream(me.getStreamLoadConfig(), dataList);

		return null;
	}

}
